package com.dongdongshop.operator.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.operator.service.ItemCatService;
import com.dongdongshop.seller.pojo.TbGoods;
import com.dongdongshop.seller.pojo.TbItem;
import com.dongdongshop.seller.service.GoodsService;
import com.dongdongshop.seller.vo.GoodsVO;
import com.dongdongshop_util.Result;
import com.dongdongshop_util.ResultEnum;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("goods")
public class GoodsController {

    private final Logger logger = LoggerFactory.getLogger(GoodsController.class);

    @Reference(check = false,timeout = 50000000)
    private GoodsService goodsService;
    @Reference(check = false,timeout = 50000000)
    private ItemCatService itemCatService;
    //注入mq
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @RequestMapping("toPage")
    public String toPage(){
        return "page";
    }
    //跳转到商品表
    @RequestMapping("toGoods")
    public String toGoods(){
        return "admin/goods";
    }


    //获取商品表
    @ResponseBody
    @RequestMapping("getGoods")
    public Result getGoods(String goodsName){
        try {
            List<TbGoods> goodsList = goodsService.getGoodsByName(goodsName);
            return Result.success(ResultEnum.SUCCESS,goodsList);
        }catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //批量删除
    @ResponseBody
    @RequestMapping("deleteGoods")
    public Result deleteGoods(@RequestParam(value = "ids[]")Long[] ids){
        try {
            goodsService.deleteGoods(ids);
            return Result.response(ResultEnum.SUCCESS);
        }catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //审核操作
    @ResponseBody
    @RequestMapping("isStatus")
    public Result isStatus(Integer obj,@RequestParam(value = "ids[]")Long[] ids){
        try {
            goodsService.updateStatus(obj,ids);
            if (obj == 1){
                for (Long goodsId : ids) {
                    rocketMQTemplate.convertAndSend("goods_goodsId",goodsId);
                    System.out.println("消息发送成功"+goodsId);
                }
            }
            return Result.response(ResultEnum.SUCCESS);

        }catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }

    //回显
    @ResponseBody
    @RequestMapping("selectById")
    public Result selectById(Long id){
        try {
            GoodsVO goodsVO = goodsService.selectById(id);
            List<TbItem> itemList = itemCatService.getItemByGoodsId(id);
            Map<String,Object> map = new HashMap<>();
            map.put("goodsVO",goodsVO);
            map.put("itemList",itemList);
            return Result.success(ResultEnum.SUCCESS,map);
        }catch (Exception e){
            logger.info(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
}
